<template>
<!-- 医疗账单录入 -->
    <div v-loading="loading">
        <div class="topsubmint el-col el-col-21">账单录入
            <el-button style="float: right; margin-right:50px;" type="primary" @click="submitForm">提交</el-button>

        </div>
        <el-card class="box-card">
            <div slot="header" class="card-heaer">
                <span style="line-height: 36px;">账单录入</span>

            </div>

            <el-form label-width="100px">
                <el-row type="flex" class="row-bg">
                    <el-col :span="5">

                        <el-form-item label="医院名称:">
                            <el-input v-model="BILL_HISPOTAL" placeholder="医院名称"></el-input>
                        </el-form-item>

                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="疾病名称:">
                            <el-input v-model="BILL_DISEASE" placeholder="疾病名称"></el-input>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row type="flex" class="row-bg">
                    <el-col :span="6">
                        <el-form-item label="疾病详情:">
                            <el-input v-model="BILL_DISEASEDETAIL" type="textarea" placeholder="疾病详情"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" class="row-bg">
                    <el-col :span="6">
                        <el-form-item label="日期:">
                            <el-date-picker type="date" placeholder="选择日期" v-model="addtime" style="width: 100%;"></el-date-picker>
                        </el-form-item>
                    </el-col>

                    <el-col :span="6">
                        <el-form-item label="医院类型:">
                            <el-radio-group v-model="BILL_DATA_TYPE">
                                <el-radio label="网络内"></el-radio>
                                <el-radio label="网络外"></el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="3">
                        <el-button type="primary" @click="adddata">新增一天</el-button>
                    </el-col>
                    <el-col :span="3">
                        <el-button type="primary" class="fileselect">
                            <input type="file" @change="upload" value="" id="fileup"> 上传附件
                        </el-button>
                    </el-col>
                </el-row>
            </el-form>
            <div class="space20">
            </div>
            <el-collapse v-if="bill.length>0">
                <el-collapse-item :title="data.show+'账单'" :key="data.show" v-for="data in bill ">

                    <el-card class="box-card">
                        <div slot="header" class="card-heaer">
                            <span style="line-height: 36px;">{{data.show}}账单</span>
                            <el-button style="float: right; margin-right:50px;" type="danger" @click="delectdata(data)">删除</el-button>
                        </div>
                        <el-form label-width="200px">
                            <el-row type="flex" class="row-bg">

                                <el-col>
                                    <el-form-item label="医院类型:">
                                        <el-radio-group v-model="data.BILL_DATA_TYPE">
                                            <el-radio label="网络内"></el-radio>
                                            <el-radio label="网络外"></el-radio>
                                        </el-radio-group>
                                    </el-form-item>
                                </el-col>

                            </el-row>
                        </el-form>
                        <div class="space20">
                        </div>
                        <liepeiup_item :billdataid="data.BILL_DATA_ID" ref="dataform">
                        </liepeiup_item>
                    </el-card>
                </el-collapse-item>
            </el-collapse>          
        </el-card>
    </div>
</template>
<script>
import { mapState } from "vuex";
import getid from "@/js/getid";
import liepeiup_item from "@/components/lipei/liepeiup_item";

export default {
  data() {
    return {
      addtime: "",
      file: "",
      loading: false,
      BILL_HISPOTAL: "",
      BILL_DATA_TYPE: "",
      BILL_DISEASEDETAIL: "",
      BILL_DISEASE: ""
    };
  },
  props: {
    //状态 新增/医师审核/理赔审核/修改
    state: {
      type: String,
      default: "新增"
    }
  },
  components: {
    liepeiup_item: liepeiup_item
  },
  computed: mapState({
    bill: state => state.bill.bill
  }),
  methods: {
    upload(event) {
      let file = event.target.files[0];
      if (file) {
        const formData = new FormData();
        formData.append("file", file);
        this.$http
          .post("ReportInfomation/file", formData, {
            progress(event) {
              //传递给父组件的progress方法
              // self.$emit('progress', parseFloat(event.loaded / event.total * 100), flag)
            }
          })
          .then(
            response => {
              var data = response.body;
              this.loading = false;
              if (data.code == 1) {
                this.file = data.msg[0];
                this.$notify({
                  title: "附件上传成功",
                  message: "附件上传成功",
                  type: "success"
                });
                document.getElementById("fileup").value = "";
              } else {
                this.loding = false;
                this.$notify.error({
                  title: "提交失败",
                  message: data.msg
                });
                document.getElementById("fileup").value = "";
              }
            },
            response => {
              this.loading = false;
              this.$notify.error({
                title: "网络错误",
                message: "网络错误"
              });
              this.loding = false;
              document.getElementById("fileup").value = "";
            }
          );
      }
    },
    submitForm() {
      if (this.bill.length == 0) {
        this.$alert("您还没有添加账单", "信息不完整", {
          confirmButtonText: "确定"
        });
        return;
      }
      for (var i = 0; i < this.$refs.dataform.length; i++) {
        for (var k = 0; k < this.$refs.dataform[i].$refs.item.length; k++) {
          var result = false;
          this.$refs.dataform[i].$refs.item[k].validate(valid => {
            if (valid) {
              result = true;
            }
          });
          if (!result) {
            this.$alert("请查看是否有没有填写的必填项", "信息不完整", {
              confirmButtonText: "确定"
            });
            return;
          }
        }
      }
      if (
        this.BILL_DISEASE == "" ||
        this.BILL_DISEASEDETAIL == "" ||
        this.BILL_HISPOTAL == ""
      ) {
        this.$alert("请将疾病信息填写完整", "信息不完整", {
          confirmButtonText: "确定"
        });
        return;
      }
      if (this.file == "") {
        this.$alert("您还没有上传附件", "信息不完整", {
          confirmButtonText: "确定"
        });
        return;
      }

      var billresult = {
        BILL_ID: getid.getid(),
        RI_MEDICAL_ID: this.$route.query["id"],
        REPORTINFORMATION_PRODUCE: this.$route.query["type"],
        REPORTINFORMATION_PRODUCEID: 0,
        BILL_FILE: this.file,
        BILL_DATA: this.bill,
        BILL_DISEASE: this.BILL_DISEASE,
        BILL_DISEASEDETAIL: this.BILL_DISEASEDETAIL,
        BILL_HISPOTAL: this.BILL_HISPOTAL
      };
      this.$http.post("Bill", billresult).then(
        response => {
          var data = response.body;
          if (data.code == 1) {
            this.bill = [];
            this.$notify({
              title: "添加成功",
              message: "添加成功，请等待审核",
              type: "success"
            });
            this.$router.go(-1);
          } else {
            this.loding = false;
            this.$notify.error({
              title: "提交失败",
              message: data.msg
            });
          }
        },
        response => {
          this.$notify.error({
            title: "网络错误",
            message: "网络错误"
          });
          this.loding = false;
        }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    delectdata(item) {
      var index = this.bill.indexOf(item);
      if (index !== -1) {
        this.bill.splice(index, 1);
      }
    },
    adddata() {
      if (this.addtime == "") {
        this.$alert("请先选择日期", "提示", {
          confirmButtonText: "确定"
        });
      } else {
        if (this.BILL_DATA_TYPE == "") {
          this.$alert("请先选择医院类型", "提示", {
            confirmButtonText: "确定"
          });
          return;
        }
        var data = this.addtime;

        var month = data.getMonth() + 1;

        var strDate = data.getDate();
        var year = data.getFullYear();
        var time = year + "-" + month + "-" + strDate;

        var have = this.timehave(time);
        if (!have) {
          this.bill.push({
            BILL_DATA_ID: getid.getid(),
            BILL_DATA_DATA: time,
            BILL_DATA_HNAME: this.BILL_DATA_HNAME,
            BILL_DATA_TYPE: this.BILL_DATA_TYPE,
            BILL_DATA_URGENCY: "否",
            show: time,
            BILL_DATA_DETAIL: []
          });
        }
        this.addtime = "";
        // this.BILL_DATA_HNAME = "";
        this.BILL_DATA_TYPE = "";
      }
    },
    timehave(time) {
      for (var i = 0; i < this.bill.length; i++) {
        if (this.bill[i].show == time) {
          return true;
        }
      }
      return false;
    }
  },
  activated() {
    //新增/医师审核/理赔审核/修改

    switch (this.state) {
      case "新增":
        this.BILL_HISPOTAL = "";
        this.BILL_DISEASEDETAIL = "";
        this.BILL_DISEASE = "";
        this.BILL_DATA_HNAME = "";
        this.file = "";
        this.bill.splice(0, this.bill.length);
        break;
      case "医师审核":
        this.bill.splice(0, this.bill.length);
        this.$http.get("Bill/" + this.$route.query["id"]).then(
          response => {
            var data = response.body;
            if (data.code == 1) {
              this.$store.commit("changer", data.result[0].BILL_DATA);
              this.BILL_ID = data.result[0].BILL_ID;
              this.file = data.result[0].BILL_FILE;
              this.BILL_STATE = "";
              this.BILL_WHY = data.result[0].BILL_WHY;
              this.BILL_DISEASE = data.result[0].BILL_DISEASE;
              this.BILL_DISEASEDETAIL = data.result[0].BILL_DISEASEDETAIL;
            } else {
            }
          },
          response => {
            this.$notify.error({
              title: "网络错误",
              message: "网络错误"
            });
            this.loding = false;
          }
        );
        break;
      case "理赔审核":
        break;
      case "修改":
        break;
      default:
        break;
    }
  }
};
</script>
<style scoped>
.fileselect {
  overflow: hidden;
  position: relative;
}

.fileselect input {
  opacity: 0;
  position: absolute;
}
</style>